package com.example.administrator.myapplication.net;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class HttpClient {

    private final int retrys;
    private Dispatcher dispatcher;
    private ConnectionPool connectionPool;
    public Dispatcher dispatcher() {
        return dispatcher;
    }

    public ConnectionPool connectionPool(){
        return connectionPool;
    }

    public int retrys(){
        return retrys;
    }

    public HttpClient(Builder builder) {
        this.dispatcher = builder.dispatcher;
        this.retrys = builder.retrys;
        this.connectionPool = builder.connectionPool;
    }

    public Call newCall(Request request) {
        return new Call(request, this);
    }

    public static final class Builder {
        Dispatcher dispatcher;
        int retrys;
        ConnectionPool connectionPool;

        public Builder dispatcher(Dispatcher dispatcher) {
            this.dispatcher = dispatcher;
            return this;
        }

        public Builder retrys(int retrys){
            this.retrys = retrys;
            return this;
        }

        public Builder connectionPool(ConnectionPool pool){
            this.connectionPool = pool;
            return this;
        }

        public HttpClient build() {
            if (connectionPool == null){
                connectionPool = new ConnectionPool(60, TimeUnit.SECONDS);
            }
            if (dispatcher == null){
                dispatcher = new Dispatcher();
            }
            return new HttpClient(this);
        }
    }
}
